package rssApp.builders 
{
	import app.IApp;
	import app.IAppBuilder;
	import com.bit101.charts.BarChart;
	import com.bit101.components.HBox;
	import com.bit101.components.InputText;
	import com.bit101.components.Panel;
	import com.bit101.components.ProgressBar;
	import com.bit101.components.PushButton;
	import com.bit101.components.ScrollPane;
	import com.bit101.components.Style;
	import com.bit101.components.VBox;
	import common.IDisposable;
	import flash.display.DisplayObjectContainer;
	import flash.display.Stage;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import rss.events.RssFeedEvent;
	import rss.IRssService;
	import rss.services.RssDummyService;
	import rss.services.RssService;
	import flash.events.Event;
	import rssApp.apps.RssApp;
	import rssApp.controllers.RssAppController;
	import rssApp.IRssAppController;
	import rssApp.IRssAppModel;
	import rssApp.IRssAppViewMediator;
	import rssApp.mediators.RssAppViewMediator;
	import rssApp.models.RssAppModel;
	
	/**
	 * Constructs an instance of an RssApp application. The application hosts a 
	 * model, view, controller, and a service which are coordinated to load and
	 * display RSS feeds in the RSS 2.0 format.
	 * 
	 * In general terms the application works as follows:
	 * 	- 	The user enters a web address of an RSS feed and clicks the "load feed" 
	 * 		button.
	 *  - 	The service loads the XML feed and uses feed builder to produce a feed 
	 * 		model which is then put into the application model.
	 * 	- 	Meanwhile, the view has been listening for changes in the model and updates
	 * 		itself to reflect the data in the feed.
	 * 
	 * Some (semi-documented) features of the application:
	 * 	-	Feed entries by ticking/un-ticking the category check boxes on the left
	 *  -	Only RSS 2.0 feeds are supported, and only the following fields are
	 * 		used: title, description, published date, link, category. If a field
	 * 		is missing, it is substituted with an empty string. If more than one
	 * 		field exists, they are concatenated into one.
	 * 
	 * @author Luke Van In
	 */
	public class RssAppBuilder implements IAppBuilder 
	{
		
		// private variables
		private var _stage:Stage;
		
		private var _displayObjectContainer:DisplayObjectContainer;
		
		

		/**
		 * Constructor
		 * 
		 * @param	stage
		 * @param	displayObjectContainer		Root display object (does not have to be stage)
		 */
		public function RssAppBuilder(stage:Stage, displayObjectContainer:DisplayObjectContainer) 
		{
			this._stage = stage;
			
			this._displayObjectContainer = displayObjectContainer;
		}
		
		
		
		
		/* INTERFACE app.IAppBuilder */
		
		/**
		 * Creates and returns an instance of an RssApp
		 * 
		 * @return
		 */
		public function buildApp():IApp 
		{
			// create a service
			var eventDispatcher:IEventDispatcher = new EventDispatcher();
			
			var service:IRssService = new RssService(eventDispatcher);
			//var service:IRssService = new RssDummyService(eventDispatcher); // TODO: remove this dummy service before publishing
			
			
			var model:IRssAppModel = new RssAppModel();
			
			
			// create the view
			
			// create root layout to contain address and content areas
			var rootLayout:VBox = new VBox(this._displayObjectContainer, 0, 0);
			
			rootLayout.spacing = 0;
			
			
			// create address layout with load button
			var addressLayout:HBox = new HBox(rootLayout, 0, 0);
			
			addressLayout.spacing = 0;
			
			var addressInput:InputText = new InputText(addressLayout, 0, 0, "http://");
			
			var loadButton:PushButton = new PushButton(addressLayout, 0, 0, "Load RSS feed");
			
			var progressbar:ProgressBar = new ProgressBar(addressLayout, 0, 0);
		

			
			// create content layout to contain feed items and categories
			var contentLayout:HBox = new HBox(rootLayout, 0, 0);
			
			contentLayout.spacing = 0;
			
			
			// create scrollpane to display feed items
			var contentScrollPane:ScrollPane = new ScrollPane(contentLayout, 0, 0);
				
			contentScrollPane.dragContent = true;
			
			contentScrollPane.autoHideScrollBar = true;
			
			var itemsLayout:VBox = new VBox(contentScrollPane, 0, 0);

			
			// create panel to display category checkboxes
			var categoriesPanel:Panel = new Panel(contentLayout, 0, 0);		
			
			var categoriesLayout:VBox = new VBox(categoriesPanel, 0, 0);
			


			// create view mediator to update the view components
			var view:IRssAppViewMediator = new RssAppViewMediator(eventDispatcher, model, this._stage, addressInput, loadButton, contentLayout, categoriesPanel, contentScrollPane, itemsLayout, categoriesLayout, progressbar);
			
			
			// create main application controller
			var controller:IRssAppController = new RssAppController(eventDispatcher, service, model);
			
			
			// create and return application context
			var app:IApp = new RssApp(model, view, controller, service);
			
			return app;
		}
		
		
		
		
		
		/**
		 * 
		 */
		public function dispose():void
		{
			
		}
		
		
	}

}